package com.lukebang.TEST;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lukebang.constant.TypeOfIndent;

/**
 * 
 * SqlTest 下午6:29:37 Copyright zhaocj Inc. All rights reserved. Love Me Like
 * Love Justin Bieber
 * 
 */
public class SqlTest {

	DBHelp db = new DBHelp();

	private static ExecutorService executorService = new ThreadPoolExecutor(28, 35, 0L, TimeUnit.MILLISECONDS,
			new LinkedBlockingQueue<Runnable>(100000), new ThreadPoolExecutor.CallerRunsPolicy());
	private static Random random = new Random();

	// 为城市的价钱表 插入数据
	@Test
	public void test1() throws Exception {
		// 查询所有的省份
		String queryProvinces = "select code,name as province from chinaAreaTwo where level = 1";
		Connection queryProvincesCon = db.getCon();
		PreparedStatement statement = queryProvincesCon.prepareStatement(queryProvinces);
		ResultSet executeQuery = statement.executeQuery();
		int i = 1;
		while (executeQuery.next()) {
			String provinceCode = executeQuery.getString("code");
			String provinceName = executeQuery.getString("province");
			String queryCites = "select code,name as city from chinaAreaTwo where parentID = " + provinceCode;
			PreparedStatement prepareStatement = queryProvincesCon.prepareStatement(queryCites);
			ResultSet executeQuery2 = prepareStatement.executeQuery();
			while (executeQuery2.next()) {
				String cityCode = executeQuery2.getString("code");
				String cityName = executeQuery2.getString("city");
				System.out.println(executeQuery2.getString("code"));
				System.out.println(executeQuery2.getString("city"));
				String insertSql = "INSERT INTO `territory` VALUES (?, ?, ?, null, 1000, '3', '100.00', '50.00', 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, '1', 3, 3, 100, 100,?)";
				PreparedStatement prepareStatement2 = queryProvincesCon.prepareStatement(insertSql);
				prepareStatement2.setInt(1, i);
				prepareStatement2.setString(2, provinceName);
				prepareStatement2.setString(3, cityName);
				prepareStatement2.setString(4, provinceCode + "/" + cityCode);
				prepareStatement2.executeUpdate();
				i = i + 1;
				String insertSql2 = "INSERT INTO `territory` VALUES (?, ?, ?, null, 1000, '3', '100.00', '50.00', 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, '0', 3, 3, 100, 100,?)";
				PreparedStatement prepareStatement3 = queryProvincesCon.prepareStatement(insertSql2);
				prepareStatement3.setInt(1, i);
				prepareStatement3.setString(2, provinceName);
				prepareStatement3.setString(3, cityName);
				prepareStatement3.setString(4, provinceCode + "/" + cityCode);
				prepareStatement3.executeUpdate();
				i = i + 1;
			}
		}
		db.closeAll(queryProvincesCon, statement, executeQuery);
	}

	public static int getRandomNumber(int min, int max) {
		int s = random.nextInt(max) % (max - min + 1) + min;
		return s;
	}
	@Test
	public void test3(){
		Map<String, String> extraMap = new HashMap<String, String>();
		extraMap.put("bussType", TypeOfIndent.MEASURE.getName());
		extraMap.put("indent", "100");
		//System.out.println(JSON.toJSON(extraMap));
		//System.out.println(JSON.toJSONString(extraMap));
		System.out.println(JSON.toJSONString(extraMap, SerializerFeature.UseSingleQuotes));
	}
	
}
